package com.example.attendance_system.service.impl;

import com.example.attendance_system.entity.LeaveRecord;
import com.example.attendance_system.repository.LeaveRecordRepository;
import com.example.attendance_system.service.LeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 请假服务实现类
 */
@Slf4j
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveRecordRepository leaveRecordRepository;

    @Override
    @Transactional
    public LeaveRecord submitLeaveApplication(LeaveRecord leaveRecord) {
        // 设置默认状态为待审批
        leaveRecord.setStatus(0);
        return leaveRecordRepository.save(leaveRecord);
    }

    @Override
    @Transactional
    public LeaveRecord approveLeaveApplication(Long id, String approverNo, Integer status, String remark) {
        Optional<LeaveRecord> optionalLeaveRecord = leaveRecordRepository.findById(id);
        if (optionalLeaveRecord.isEmpty()) {
            throw new IllegalArgumentException("请假记录不存在");
        }

        LeaveRecord leaveRecord = optionalLeaveRecord.get();
        // 只能审批待审批状态的请假记录
        if (leaveRecord.getStatus() != 0) {
            throw new IllegalStateException("该请假申请已被处理，无法再次审批");
        }

        // 防止管理员审批自己的请假记录
        if (leaveRecord.getEmployeeNo().equals(approverNo)) {
            throw new IllegalStateException("管理员不能审批自己的请假申请");
        }

        // 更新审批信息
        leaveRecord.setStatus(status);
        leaveRecord.setApproverNo(approverNo);
        leaveRecord.setApprovalTime(LocalDateTime.now());
        leaveRecord.setApprovalRemark(remark);

        return leaveRecordRepository.save(leaveRecord);
    }

    @Override
    public List<LeaveRecord> getEmployeeLeaveRecords(String employeeNo) {
        return leaveRecordRepository.findByEmployeeNo(employeeNo);
    }

    @Override
    public Page<LeaveRecord> getEmployeeLeaveRecords(String employeeNo, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNo(employeeNo, pageable);
    }

    @Override
    public Page<LeaveRecord> getEmployeeLeaveRecordsByStatus(String employeeNo, Integer status, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNoAndStatus(employeeNo, status, pageable);
    }

    @Override
    public Page<LeaveRecord> getAllLeaveRecords(Pageable pageable) {
        return leaveRecordRepository.findAll(pageable);
    }

    @Override
    public Page<LeaveRecord> getAllLeaveRecordsExcludeEmployee(String excludeEmployeeNo, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNoNot(excludeEmployeeNo, pageable);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByStatus(Integer status, Pageable pageable) {
        return leaveRecordRepository.findByStatus(status, pageable);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByStatusExcludeEmployee(Integer status, String excludeEmployeeNo, Pageable pageable) {
        return leaveRecordRepository.findByStatusAndEmployeeNoNot(status, excludeEmployeeNo, pageable);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByEmployeeNo(String employeeNo, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNo(employeeNo, pageable);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByEmployeeNoAndStatus(String employeeNo, Integer status, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNoAndStatus(employeeNo, status, pageable);
    }

    @Override
    public boolean hasOverlappingLeave(String employeeNo, LocalDate startDate, LocalDate endDate) {
        return leaveRecordRepository.hasOverlappingLeave(employeeNo, startDate, endDate);
    }

    @Override
    public List<LeaveRecord> getPendingLeaveRecords() {
        return leaveRecordRepository.findByEmployeeNoAndStatus(null, 0);
    }

    @Override
    public boolean isEmployeeOnLeave(String employeeNo, LocalDate date) {
        return leaveRecordRepository.hasApprovedLeave(employeeNo, date);
    }

    @Override
    public List<String> getEmployeesOnLeave(LocalDate date) {
        return leaveRecordRepository.findEmployeesOnLeave(date);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByEmployeeNameExcludeEmployee(String employeeName, String excludeEmployeeNo, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNameContainingAndEmployeeNoNot(employeeName, excludeEmployeeNo, pageable);
    }

    @Override
    public Page<LeaveRecord> getLeaveRecordsByEmployeeNameAndStatusExcludeEmployee(String employeeName, Integer status, String excludeEmployeeNo, Pageable pageable) {
        return leaveRecordRepository.findByEmployeeNameContainingAndStatusAndEmployeeNoNot(employeeName, status, excludeEmployeeNo, pageable);
    }
} 